import * as zrender from 'zrender';
import { ScheduleBar } from './schedule-bar';

/**
 * 人员行组件
 * 负责渲染单行的人员信息和排班数据
 */
export class PersonRow {
  private zr: any;
  private schedule: PersonSchedule;
  private chartState: ChartState;
  private y: number;
  private rowIndex: number;

  private rowGroup: zrender.Group;
  private scheduleBars: ScheduleBar[] = [];

  constructor(
    zr: any,
    schedule: PersonSchedule,
    chartState: ChartState,
    y: number,
    rowIndex: number,
  ) {
    this.zr = zr;
    this.schedule = schedule;
    this.chartState = chartState;
    this.y = y;
    this.rowIndex = rowIndex;
    this.rowGroup = new zrender.Group();
  }

  /**
   * 渲染人员行
   */
  render() {
    this.rowGroup.removeAll();
    this.scheduleBars = [];

    // const { responsiveConfig } = this.chartState;
    // const { personColumnWidth } = responsiveConfig;

    // 绘制人员信息单元格
    this.drawPersonCell();

    // 绘制排班条
    this.drawScheduleBars();

    // 添加到画布
    this.zr.add(this.rowGroup);
  }

  /**
   * 绘制人员信息单元格
   */
  private drawPersonCell() {
    const { responsiveConfig } = this.chartState;
    const { personColumnWidth, barHeight } = responsiveConfig;

    // 人员信息背景
    const background = new zrender.Rect({
      shape: {
        x: 0,
        y: this.y,
        width: personColumnWidth,
        height: barHeight,
      },
      style: {
        fill: '#ffffff',
        stroke: '#dee2e6',
        lineWidth: 1,
      },
    });

    // 人员姓名
    const nameText = new zrender.Text({
      style: {
        text: this.schedule.person.name,
        x: 15,
        y: this.y + barHeight / 2,
        verticalAlign: 'middle',
        fontSize: 12,
        fill: '#495057',
      },
    });

    // 工号（如果有）
    if (this.schedule.person.employeeId) {
      const idText = new zrender.Text({
        style: {
          text: `#${this.schedule.person.employeeId}`,
          x: personColumnWidth - 15,
          y: this.y + barHeight / 2,
          verticalAlign: 'middle',
          fontSize: 10,
          fill: '#6c757d',
        },
      });
      this.rowGroup.add(idText);
    }

    this.rowGroup.add(background);
    this.rowGroup.add(nameText);
  }

  /**
   * 绘制排班条
   */
  private drawScheduleBars() {
    const { responsiveConfig } = this.chartState;
    const { personColumnWidth, unitWidth, barHeight } = responsiveConfig;

    this.schedule.shifts.forEach(shift => {
      const scheduleBar = new ScheduleBar(
        this.zr,
        shift,
        this.chartState,
        this.y,
        personColumnWidth,
        unitWidth,
        barHeight,
      );

      scheduleBar.render();
      this.scheduleBars.push(scheduleBar);
    });
  }

  /**
   * 更新行数据
   */
  update(schedule: PersonSchedule, chartState: ChartState, y: number) {
    this.schedule = schedule;
    this.chartState = chartState;
    this.y = y;
  }

  /**
   * 获取行索引
   */
  getRowIndex(): number {
    return this.rowIndex;
  }

  /**
   * 设置行索引
   */
  setRowIndex(index: number) {
    this.rowIndex = index;
  }
}
